#include "StdAfx.h"
#include ".\mfcwpfinterface.h"

CMFCWPFInterface::CMFCWPFInterface(void)
{
	m_pMFCWPFEleCtrl = new CMFCWPFEleCtrl;
	m_pMFCWPFEleCtrl->m_fXRatio = 1.0f;
	m_pMFCWPFEleCtrl->m_fYRatio = 1.0f;
	m_pCDCPlus = new CDCPlus(&m_oFontLib,&m_oPenLib,&m_oBrushLib);
}

CMFCWPFInterface::~CMFCWPFInterface(void)
{
	if (m_pMFCWPFEleCtrl)
	{
		delete m_pMFCWPFEleCtrl;
		m_pMFCWPFEleCtrl = NULL;
	}
	for (map<CString, CMFCWPFPage *>::iterator iter = m_mapMFCWPFPage.begin();
		iter != m_mapMFCWPFPage.end();
		++iter)
	{
		CMFCWPFPage * pMFCWPFPage = iter->second;
		delete pMFCWPFPage;
	}
}

///<summary>
///load a xaml file and construct a page element and add it in m_mapMFCWPFPage
///</summary>
///<param name='szFileName'>
///the xaml file
///</param>
///<returns>
///true if add successfully; false otherwise
///</returns>
bool CMFCWPFInterface::AddFile(const CString & szFileName)
{
	CECGXmlParser oXamlParser;
	int nRet = oXamlParser.LoadFile(szFileName);
	if (ERROR_ECGXML_SUCCESS == nRet)
	{
		CString szHeight;
		CString szWidth;
		CECGXmlNode oNode;
		oXamlParser.GetNodeAttributeValue(_ROOTNODEPAGE, _ATTRIBUTEHEIGHT, szHeight);
		oXamlParser.GetNodeAttributeValue(_ROOTNODEPAGE, _ATTRIBUTEWIDTH, szWidth);

		CRect oRect(0, 0, _wtoi((wchar_t *)szWidth.GetString()), _wtoi((wchar_t *)szHeight.GetString()));
		CMFCWPFPage * pMFCWPFPage = new CMFCWPFPage(szFileName, oRect);
		if (m_mapMFCWPFPage.insert(pair<CString, CMFCWPFPage *>(szFileName, pMFCWPFPage)).second)
		{
			m_vecFileOrder.push_back(szFileName);
			m_iterDisplayFile = m_vecFileOrder.begin();
			return true;
		}
	}
	return false;
}

///<summary>
///draw one page in the whole interface
///</summary>
///<param name='hDC'>
///the display context
///</param>
void CMFCWPFInterface::DrawSelf(HDC hDC)
{
	m_pCDCPlus->AttachPlus(hDC);
	m_pCDCPlus->SetRatioPlus(m_pMFCWPFEleCtrl->m_fXRatio, m_pMFCWPFEleCtrl->m_fYRatio);
	m_mapMFCWPFPage[*m_iterDisplayFile]->DrawSelf(m_pCDCPlus);
	m_pMFCWPFEleCtrl->ResetCtrlRect();
	m_pCDCPlus->DetachPlus();
}

///<summary>
///draw the next page in the whole interface
///</summary>
///<returns>
///true if this is not the last page in the interface; false it is 
///</returns>
bool CMFCWPFInterface::PageDown()
{
	vector<CString>::iterator iter = m_iterDisplayFile;
	m_iterDisplayFile++;
	if (m_iterDisplayFile != m_vecFileOrder.end())
	{
		return true;
	}
	else
	{
		m_iterDisplayFile = iter;
		return false;
	}
}

///<summary>
///draw the last page in the whole interface
///</summary>
///<returns>
///true if this is not the first page in the interface; false it is 
///</returns>
bool CMFCWPFInterface::PageUp()
{
	if (m_iterDisplayFile != m_vecFileOrder.begin())
	{
		m_iterDisplayFile--;
		return true;
	}
	else
	{
		return false;
	}
}

///<summary>
///draw the page according to the page number in the whole interface
///</summary>
///<param name='nPageNum'>
///the page number to display
///</param>
void CMFCWPFInterface::TurnToNumber(int nPageNum)
{
	m_iterDisplayFile = m_vecFileOrder.begin() + nPageNum - 1;
}

///<summary>
///calculate the click happened in the interface according to the ratio 
///</summary>
///<param name='nFlags'>
///key pushing down flag
///</param>
///<param name='point'>
///the click point
///</param>
///<param name='pWnd'>
///the window in which click happened 
///</param>
///<returns>
///true if there is change in the interface; false there is not
///</returns>
bool CMFCWPFInterface::MouseLeftButtonUp(UINT nFlags, CPoint point, CWnd * pWnd)
{
	CPoint oPoint((int)(point.x/m_pMFCWPFEleCtrl->m_fXRatio), (int)(point.y/m_pMFCWPFEleCtrl->m_fYRatio));
	return m_pMFCWPFEleCtrl->MouseLeftButtonUp(m_mapMFCWPFPage[*m_iterDisplayFile]->MouseLeftButtonUp(nFlags, oPoint), pWnd);
}

///<summary>
///change ratio 
///</summary>
///<param name='fXRatio'>
///x ratio
///</param>
///<param name='fYRatio'>
///y ratio
///</param>
void CMFCWPFInterface::ChangeRatio(float fXRatio, float fYRatio)
{
	m_pMFCWPFEleCtrl->m_fXRatio += fXRatio;
	m_pMFCWPFEleCtrl->m_fYRatio += fYRatio;
}

///<summary>
///Transform the xml file to xaml file
///</summary>
///<param name='szFileName'>
///the xml file name
///</param>
void CMFCWPFInterface::Transform(const CString & szFileName)
{
	CECGXmlParser oXamlParser;
	if (ERROR_ECGXML_SUCCESS == oXamlParser.LoadFile(szFileName))
	{
		CString szHeight;
		CString szWidth;
		CECGXmlNode oNode;
		oXamlParser.GetNodeAttributeValue(_ROOTNODEPAGE, _ATTRIBUTEHEIGHT, szHeight);
		oXamlParser.GetNodeAttributeValue(_ROOTNODEPAGE, _ATTRIBUTEWIDTH, szWidth);
		int nWidth = _wtoi((wchar_t *)szWidth.GetString());
		int nHeight = _wtoi((wchar_t *)szHeight.GetString());

		CString szFileOut = szFileName + CString(_T(".xaml"));
		CStdioFile oStdioFile(szFileOut, CStdioFile::modeWrite|CStdioFile::modeCreate);
		CString szPath = _T("/Page");
		oStdioFile.WriteString(CString(_T("<Page ")) + _T("Width=\"") + szWidth + _T("\" ") + _T("Height=\"") + szHeight + _T("\">\n"));
		int nChildCount = oXamlParser.GetChildNodeCount(szPath);
		for (int nChildIndex = 0; nChildIndex < nChildCount; ++nChildIndex)
		{
			oXamlParser.ParseChildNode(oNode, szPath, nChildIndex);
			CString szName = oNode.GetName();
			CString szTextOut;
			if (szName == _T("Text") || szName == _T("Static"))
			{
				szTextOut = _T("<Label ");
				TransformAttributes(oNode, szTextOut, nWidth, nHeight);
				if (oNode.GetContent() == _T(""))
				{
					szTextOut += _T("/>\n");
				}
				else
				{
					szTextOut += _T(">") + oNode.GetContent() + _T("</Label>\n");
				}
			}
			if (szName == _T("Ctrl"))
			{
				szTextOut = _T("<TextBox ");
				TransformAttributes(oNode, szTextOut, nWidth, nHeight);
				if (oNode.GetContent() == _T(""))
				{
					szTextOut += _T("/>\n");
				}
				else
				{
					szTextOut += _T(">") + oNode.GetContent() + _T("</TextBox>\n");
				}
			}
			if (szName == _T("PHRTrend"))
			{
				szTextOut = _T("<Image ");
				TransformAttributes(oNode, szTextOut, nWidth, nHeight);
				if (oNode.GetContent() == _T(""))
				{
					szTextOut += _T("/>\n");
				}
				else
				{
					szTextOut += _T(">") + oNode.GetContent() + _T("</Image>\n");
				}
			}
			if (szName == _T("Line"))
			{
				szTextOut = _T("<Line ");
				int nAttributeCount = oNode.GetAttributesCount();
				for (int nAttributeIndex = 0; nAttributeIndex < nAttributeCount; ++nAttributeIndex)
				{
					if (oNode.GetAttribute(nAttributeIndex)->GetName() == _T("StartX"))
					{
						szTextOut +=  _T("X1=\"");
						szTextOut += oNode.GetAttribute(nAttributeIndex)->GetValue() + _T("\" ");
					}
					if (oNode.GetAttribute(nAttributeIndex)->GetName() == _T("StartY"))
					{
						szTextOut +=  _T("Y1=\"");
						szTextOut += oNode.GetAttribute(nAttributeIndex)->GetValue() + _T("\" ");
					}
					if (oNode.GetAttribute(nAttributeIndex)->GetName() == _T("EndX"))
					{
						szTextOut +=  _T("X2=\"");
						szTextOut += oNode.GetAttribute(nAttributeIndex)->GetValue() + _T("\" ");
					}
					if (oNode.GetAttribute(nAttributeIndex)->GetName() == _T("EndY"))
					{
						szTextOut +=  _T("Y2=\"");
						szTextOut += oNode.GetAttribute(nAttributeIndex)->GetValue() + _T("\" ");
						szTextOut +=  _T("Stroke=\"Black\" ");
					}
					if (oNode.GetAttribute(nAttributeIndex)->GetName() == _T("Width"))
					{
						szTextOut +=  _T("StrokeThickness=\"");
						szTextOut += oNode.GetAttribute(nAttributeIndex)->GetValue() + _T("\" ");
					}
				}
				szTextOut += _T("/>\n");
			}
			CStringA szTextOutA(szTextOut.GetString());
			oStdioFile.Write(szTextOutA.GetString(), szTextOutA.GetLength());
			//oStdioFile.WriteString(szTextOutA);
		}
		oStdioFile.WriteString(_T("</Page>\n"));
		oStdioFile.Close();
	}
}

void CMFCWPFInterface::TransformAttributes(CECGXmlNode & oNode, CString & szTextOut, int nWidth, int nHeight)
{
	CString szTemp;
	CString szLeft, szTop, szID, szWidth, szHeight;
	int n1, n2;
	int nAttributeCount = oNode.GetAttributesCount();
	for (int nAttributeIndex = 0; nAttributeIndex < nAttributeCount; ++nAttributeIndex)
	{
		if (oNode.GetAttribute(nAttributeIndex)->GetName() == _T("Left"))
		{
			szLeft = oNode.GetAttribute(nAttributeIndex)->GetValue();
			szTextOut += CString(_T("Margin=\"")) + szLeft + CString(_T(","));
		}
		if (oNode.GetAttribute(nAttributeIndex)->GetName() == _T("Top"))
		{
			szTop = oNode.GetAttribute(nAttributeIndex)->GetValue();
			szTextOut += szTop + CString(_T(","));
		}
		if (oNode.GetAttribute(nAttributeIndex)->GetName() == _T("Width"))
		{
			szWidth = oNode.GetAttribute(nAttributeIndex)->GetValue();
			n1 = _wtoi((wchar_t *)szLeft.GetString());
			n2 = _wtoi((wchar_t *)szWidth.GetString());
			szTemp.Format(_T("%d"), nWidth-n1-n2);
			szTextOut += szTemp + CString(_T(","));
		}
		if (oNode.GetAttribute(nAttributeIndex)->GetName() == _T("Height"))
		{
			szHeight = oNode.GetAttribute(nAttributeIndex)->GetValue();
			n1 = _wtoi((wchar_t *)szTop.GetString());
			n2 = _wtoi((wchar_t *)szHeight.GetString());
			szTemp.Format(_T("%d"), nHeight-n1-n2);
			szTextOut += szTemp + CString(_T("\" "));
		}
		if (oNode.GetAttribute(nAttributeIndex)->GetName() == _T("ID"))
		{
			szID = oNode.GetAttribute(nAttributeIndex)->GetValue();
			szTextOut += CString(_T("Name=\"")) + szID + CString(_T("\" "));
		}
	}
}